package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.company.EntityCompanyConfig;
import com.ocom.common.entity.payconfig.EntityXfDiscountConfig;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoXfDiscountConfig;
import com.ocom.payconfig.service.CompanyConfigService;
import com.ocom.payconfig.service.XfDiscountConfigService;
import com.ocom.redis.aspect.CleanRedisAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;


@Service
public class XfDiscountConfigServiceImpl extends ServiceImpl<IDaoXfDiscountConfig, EntityXfDiscountConfig> implements XfDiscountConfigService {


    @Autowired
    IDaoXfDiscountConfig iDaoXfDiscountConfig;

    @Autowired
    CompanyConfigService companyConfigService;

    /**
     * 列表
     *
     * @param companyId
     * @param accountType
     * @return
     */
    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityXfDiscountConfig> getConsDiscountConfig(Long companyId, Integer accountType, Integer consTimeZone) {
//		QueryWrapper<EntityXfDiscountConfig> queryWrapper = Wrappers.query();
//		queryWrapper.eq("com_id", companyId);
//		if (accountType != null && accountType > 0) {
//			queryWrapper.eq("acc_type_id", accountType);
//		}
//		if (consTimeZone != null && consTimeZone > 0) {
//			queryWrapper.eq("cons_time_zone", consTimeZone);
//		}
//		queryWrapper.orderByAsc("cons_type", "meal_no", "cons_times");
//		return this.list(queryWrapper);
        return iDaoXfDiscountConfig.discountConfigList(companyId, accountType, consTimeZone);
    }

    @Override
    @CleanRedisAspect(prefix = "xf_discount_config", element = {"comId", "accTypeId", "consType", "mealNo"})
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.MASTER)
    public EntityXfDiscountConfig setConsDiscountConfig(Long comId,
                                                        Integer accTypeId,
                                                        Integer consType,
                                                        Integer mealNo,
                                                        Integer consTimes,
                                                        Integer consTimeZone,
                                                        Integer merchId,
                                                        EntityXfDiscountConfig entityXfDiscountConfig) {
        QueryWrapper<EntityXfDiscountConfig> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", comId);
        queryWrapper.eq("cons_type", consType);
        queryWrapper.eq("meal_no", mealNo);
        queryWrapper.eq("acc_type_id", accTypeId);
        queryWrapper.eq("cons_times", consTimes);
        if (consTimeZone == null) {
            consTimeZone = 0;
        }
        queryWrapper.eq("cons_time_zone", consTimeZone);
        queryWrapper.eq("merch_id", merchId);
        EntityXfDiscountConfig entityXfDiscountConfig1 = this.getOne(queryWrapper);
        if (entityXfDiscountConfig1 == null) {
            Boolean b = this.save(entityXfDiscountConfig);
            if (b) {
                return entityXfDiscountConfig;
            }
            return null;
        } else {
            UpdateWrapper<EntityXfDiscountConfig> updateWrapper = Wrappers.update();
            updateWrapper.eq("com_id", comId);
            updateWrapper.eq("cons_type", consType);
            updateWrapper.eq("meal_no", mealNo);
            updateWrapper.eq("acc_type_id", accTypeId);
            updateWrapper.eq("cons_times", consTimes);
            updateWrapper.eq("cons_time_zone", consTimeZone);
            updateWrapper.eq("merch_id", merchId);
            updateWrapper.set("discount_mode", entityXfDiscountConfig.getDiscountMode());
            if (entityXfDiscountConfig.getDiscountMode() == 0) {
                updateWrapper.set("discount_value", 0);
            } else {
                updateWrapper.set("discount_value", entityXfDiscountConfig.getDiscountValue());
            }
            updateWrapper.set("update_user", entityXfDiscountConfig.getUpdateUser());

            Boolean b = this.update(updateWrapper);
            if (b) {
                return entityXfDiscountConfig;
            }
            return null;
        }
    }

    @Override
//	@CleanRedisAspect(prefix = "xf_discount_config", element = {"comId", "accTypeId", "consType", "mealNo","mealList"})
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.MASTER)
    public Result setConsDiscountConfigB(Long comId, EntityXfDiscountConfig entityXfDiscountConfig) {
        QueryWrapper<EntityXfDiscountConfig> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", comId);
        queryWrapper.eq("cons_type", entityXfDiscountConfig.getConsType());
        queryWrapper.eq("acc_type_id", entityXfDiscountConfig.getAccTypeId());
        Integer mealNo = entityXfDiscountConfig.getMealNo();
        if (mealNo >= 1 && mealNo <= 99) {
            queryWrapper.eq("meal_no", 100);
            queryWrapper.like("meal_list", mealNo);
            EntityXfDiscountConfig entityXfDiscountConfig1 = this.getOne(queryWrapper);
            if (entityXfDiscountConfig1 != null) {
                return ResultUtil.error(ResultEnum.Err_3301);
            }
        } else if (mealNo == 100) {
            String[] split = entityXfDiscountConfig.getMealList().split(",");
            List<String> mealList = Arrays.asList(split);
            queryWrapper.and(wrapper -> {
                wrapper.or().in("meal_no", mealList);
                wrapper.or(wrapper1 -> {
                    wrapper1.or().eq("meal_no", 100).ne("meal_list", entityXfDiscountConfig.getMealList());
                });
            });
            EntityXfDiscountConfig entityXfDiscountConfig1 = this.getOne(queryWrapper);
            if (entityXfDiscountConfig1 != null) {
                return ResultUtil.error(ResultEnum.Err_3301);
            }
        }
        if( entityXfDiscountConfig.getMealNo() ==600){
            QueryWrapper<EntityXfDiscountConfig> queryWrapper1 = Wrappers.query();
            queryWrapper1.eq("com_id", comId);
            queryWrapper1.eq("cons_type", entityXfDiscountConfig.getConsType());
            queryWrapper1.eq("acc_type_id", entityXfDiscountConfig.getAccTypeId());
            queryWrapper1.eq("meal_no", entityXfDiscountConfig.getMealNo());
            Integer consTimeZone = entityXfDiscountConfig.getConsTimeZone();
            if (consTimeZone == null) {
                consTimeZone = 0;
            }
            queryWrapper1.eq("cons_time_zone", consTimeZone);


            QueryWrapper<EntityXfDiscountConfig> queryWrapper2 = queryWrapper1.clone();
            queryWrapper2.eq("discount_value", entityXfDiscountConfig.getDiscountValue());
            queryWrapper2.ne("amount",entityXfDiscountConfig.getAmount());
            EntityXfDiscountConfig entityXfDiscountConfig2 = this.getOne(queryWrapper2);
            if(entityXfDiscountConfig2!=null){
                return ResultUtil.error(ResultEnum.Err_3302);
            }

            queryWrapper1.eq("amount",entityXfDiscountConfig.getAmount());

            EntityXfDiscountConfig entityXfDiscountConfig1 = this.getOne(queryWrapper1);
            if (entityXfDiscountConfig1 == null) {
                Boolean b = this.save(entityXfDiscountConfig);
                if (b) {
                    return ResultUtil.success();
                }
            } else {
                UpdateWrapper<EntityXfDiscountConfig> updateWrapper = Wrappers.update();
                updateWrapper.eq("com_id", comId);
                updateWrapper.eq("cons_type", entityXfDiscountConfig.getConsType());
                updateWrapper.eq("meal_no", mealNo);
                updateWrapper.eq("acc_type_id", entityXfDiscountConfig.getAccTypeId());
                updateWrapper.set("discount_value", entityXfDiscountConfig.getDiscountValue());
                updateWrapper.eq("cons_time_zone", consTimeZone);
                updateWrapper.set("cons_times", entityXfDiscountConfig.getConsTimes());
                updateWrapper.set("discount_mode", entityXfDiscountConfig.getDiscountMode());
                if (entityXfDiscountConfig.getDiscountMode() == 0) {
                    updateWrapper.set("discount_value", 0);
                } else {
                    updateWrapper.set("discount_value", entityXfDiscountConfig.getDiscountValue());
                }

                updateWrapper.eq("amount", entityXfDiscountConfig.getAmount());

                Boolean b = this.update(updateWrapper);
                if (b) {
                    return ResultUtil.success();
                }

            }
            return ResultUtil.error(ResultEnum.Err_3301);
        }else{
            QueryWrapper<EntityXfDiscountConfig> queryWrapper1 = Wrappers.query();
            queryWrapper1.eq("com_id", comId);
            queryWrapper1.eq("cons_type", entityXfDiscountConfig.getConsType());
            queryWrapper1.eq("acc_type_id", entityXfDiscountConfig.getAccTypeId());
            queryWrapper1.eq("meal_no", entityXfDiscountConfig.getMealNo());
            queryWrapper1.eq("cons_times", entityXfDiscountConfig.getConsTimes());
            Integer consTimeZone = entityXfDiscountConfig.getConsTimeZone();
            if (consTimeZone == null) {
                consTimeZone = 0;
            }
            queryWrapper1.eq("cons_time_zone", consTimeZone);

            EntityXfDiscountConfig entityXfDiscountConfig1 = this.getOne(queryWrapper1);
            if (entityXfDiscountConfig1 == null) {
                Boolean b = this.save(entityXfDiscountConfig);
                if (b) {
                    return ResultUtil.success();
                }
            } else {
                UpdateWrapper<EntityXfDiscountConfig> updateWrapper = Wrappers.update();
                updateWrapper.eq("com_id", comId);
                updateWrapper.eq("cons_type", entityXfDiscountConfig.getConsType());
                updateWrapper.eq("meal_no", mealNo);
                updateWrapper.eq("acc_type_id", entityXfDiscountConfig.getAccTypeId());
                updateWrapper.eq("cons_times", entityXfDiscountConfig.getConsTimes());
                updateWrapper.eq("cons_time_zone", consTimeZone);

                updateWrapper.set("discount_mode", entityXfDiscountConfig.getDiscountMode());
                if (entityXfDiscountConfig.getDiscountMode() == 0) {
                    updateWrapper.set("discount_value", 0);
                } else {
                    updateWrapper.set("discount_value", entityXfDiscountConfig.getDiscountValue());
                }
                if (entityXfDiscountConfig.getAmount() != null) {
                    updateWrapper.set("amount", entityXfDiscountConfig.getAmount());
                }
                Boolean b = this.update(updateWrapper);
                if (b) {
                    return ResultUtil.success();

                }

            }
            return ResultUtil.error(ResultEnum.Err_3301);
        }

    }


    @Override
    @CleanRedisAspect(prefix = "xf_discount_config", element = {"comId", "accTypeId", "consType", "mealNo"})
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.MASTER)
    public Boolean removeConsDiscountConfig(Long comId,
                                            Integer accTypeId,
                                            Integer consType,
                                            Integer mealNo,
                                            Integer consTimes,
                                            Integer consTimeZone,
                                            Integer merchId,
                                            Integer discountValue) {
        QueryWrapper<EntityXfDiscountConfig> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", comId);
        queryWrapper.eq("cons_type", consType);
        queryWrapper.eq("acc_type_id", accTypeId);
        queryWrapper.eq("meal_no", mealNo);
        if(mealNo==600){
            queryWrapper.eq("discount_value", discountValue);
        }else {
            queryWrapper.eq("cons_times", consTimes);
        }
        if (null != merchId) {
            queryWrapper.eq("merch_id", merchId);
        }
        if (consTimeZone != null) {
            queryWrapper.eq("cons_time_zone", consTimeZone);
        }

        return this.remove(queryWrapper);
    }

}
